package com.zadtek.shopmall.activities;

import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Switch;
import android.widget.TextView;

import com.zadtek.shopmall.R;
import com.zadtek.shopmall.base.AbsActivity;
import com.zadtek.shopmall.bean.UpdateVersionBean;
import com.zadtek.shopmall.net.BaseObserver;
import com.zadtek.shopmall.net.BaseResponse;
import com.zadtek.shopmall.net.NetTool;
import com.zadtek.shopmall.other.DataCleanManager;
import com.zadtek.shopmall.other.DialogManger;
import com.zadtek.shopmall.utils.ToastUtil;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

import static com.zadtek.shopmall.other.Const.PACKGE_NAME;

public class SystemSettingActivity extends AbsActivity implements View.OnClickListener {
    private TextView tvAbout;
    private TextView tvUserguide;
    private TextView achor;
    private Switch sw;
    private TextView tvUpdate;
    private TextView tvClean;
    private TextView tvPrivacy;
    private LinearLayout llClean;
    private TextView topTitle;
    private ImageView icBack;
    @Override
    public int setView() {
        return R.layout.activity_system_setting;
    }

    @Override
    public void initBasic(Bundle savedInstanceState) {
        initView();
        initLogic();
    }

    private void initLogic() {
        icBack.setOnClickListener(this);
        tvAbout.setOnClickListener(this);
        tvUserguide.setOnClickListener(this);
        tvUpdate.setOnClickListener(this);
        llClean.setOnClickListener(this);
        tvPrivacy.setOnClickListener(this);
        try {
            String size = DataCleanManager.getTotalCacheSize(this);
            tvClean.setText(size);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void initView() {
        tvAbout = (TextView)findViewById( R.id.tv_about );
        tvUserguide = (TextView)findViewById( R.id.tv_userguide );
        tvPrivacy = (TextView)findViewById( R.id.tv_privacy_policy );
        achor = (TextView)findViewById( R.id.achor );
        sw = (Switch)findViewById( R.id.sw );
        tvUpdate = (TextView)findViewById( R.id.tv_update );
        tvClean = (TextView)findViewById( R.id.tv_clean );
        llClean = findViewById(R.id.ll_clean);
        topTitle = findViewById(R.id.title);
        topTitle.setText("系统设置");
        icBack = findViewById(R.id.ic_back);
        icBack.setVisibility(View.VISIBLE);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.ic_back:
                finish();
                break;
            case R.id.ll_clean:
                DataCleanManager.clearAllCache(this);
                tvClean.setText("0k");
                ToastUtil.showToast("已清除！");
                break;
            case R.id.tv_about:
                Intent intent = new Intent(this,WebActivity.class);
                intent.putExtra(WebActivity.URL,"http://xmlife.zadtek.com/mobile/index.php?m=article&a=detail&id=61");
                intent.putExtra(WebActivity.TITLE,"关于点吧");
                startActivity(intent);
                break;
            case R.id.tv_userguide:
                Intent intent2 = new Intent(this,WebActivity.class);
                intent2.putExtra(WebActivity.URL,"http://xmlife.zadtek.com/mobile/index.php?m=article&a=detail&id=10");
                intent2.putExtra(WebActivity.TITLE,"用户指南");
                startActivity(intent2);
                break;
            case R.id.tv_privacy_policy:
                Intent intent3 = new Intent(this,WebActivity.class);
                intent3.putExtra(WebActivity.URL,"http://xmlife.zadtek.com/mobile/index.php?m=article&a=detail&id=2");
                intent3.putExtra(WebActivity.TITLE,"隐私政策");
                startActivity(intent3);
                break;
            case R.id.tv_update:
                checkUpdate();
                break;
        }
    }

    private void checkUpdate() {
        NetTool.getApi().checkUpdate().subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<BaseResponse<UpdateVersionBean>>() {
                    @Override
                    public void onData(BaseResponse<UpdateVersionBean> value) {
                        if(value.getCode() == 0 && value.getData()!=null){

                            PackageInfo packageInfo = null;
                            try {
                                packageInfo = getPackageManager().getPackageInfo(PACKGE_NAME,0);
                            } catch (PackageManager.NameNotFoundException e) {
                                e.printStackTrace();
                            }
                            int versionCode = packageInfo.versionCode;
                            DialogManger.showUpdate(SystemSettingActivity.this,value.getData());
                            if(value.getData().getVersioncode()>versionCode){
                                DialogManger.showUpdate(SystemSettingActivity.this,value.getData());
                            }else{
                                ToastUtil.showToast("已经是最新版本！");
                            }
                        }
                    }
                });
    }
}
